<template>
  <el-dialog
    :title="!dataForm.descId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="订金" prop="submoney">
      <el-input v-model="dataForm.submoney" placeholder="订金"></el-input>
    </el-form-item>
    <el-form-item label="入团时间" prop="joinTime">
      <el-input v-model="dataForm.joinTime" placeholder="入团时间"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="团购id" prop="groupId">
      <el-input v-model="dataForm.groupId" placeholder="团购id"></el-input>
    </el-form-item>
    <el-form-item label="户型id" prop="unitId">
      <el-input v-model="dataForm.unitId" placeholder="户型id"></el-input>
    </el-form-item>
    <el-form-item label="户型名称" prop="unitName">
      <el-input v-model="dataForm.unitName" placeholder="户型名称"></el-input>
    </el-form-item>
    <el-form-item label="用户状态 0.参团中 1.退团中 2.已线下完结" prop="status">
      <el-input v-model="dataForm.status" placeholder="用户状态 0.参团中 1.退团中 2.已线下完结"></el-input>
    </el-form-item>
    <el-form-item label="退团理由" prop="backDesc">
      <el-input v-model="dataForm.backDesc" placeholder="退团理由"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          descId: 0,
          userId: '',
          userName: '',
          submoney: '',
          joinTime: '',
          phone: '',
          groupId: '',
          unitId: '',
          unitName: '',
          status: '',
          backDesc: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          submoney: [
            { required: true, message: '订金不能为空', trigger: 'blur' }
          ],
          joinTime: [
            { required: true, message: '入团时间不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          groupId: [
            { required: true, message: '团购id不能为空', trigger: 'blur' }
          ],
          unitId: [
            { required: true, message: '户型id不能为空', trigger: 'blur' }
          ],
          unitName: [
            { required: true, message: '户型名称不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '用户状态 0.参团中 1.退团中 2.已线下完结不能为空', trigger: 'blur' }
          ],
          backDesc: [
            { required: true, message: '退团理由不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.descId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.descId) {
            this.$http({
              url: this.$http.adornUrl(`/tgroup/groupdesc/info/${this.dataForm.descId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.groupDesc.userId
                this.dataForm.userName = data.groupDesc.userName
                this.dataForm.submoney = data.groupDesc.submoney
                this.dataForm.joinTime = data.groupDesc.joinTime
                this.dataForm.phone = data.groupDesc.phone
                this.dataForm.groupId = data.groupDesc.groupId
                this.dataForm.unitId = data.groupDesc.unitId
                this.dataForm.unitName = data.groupDesc.unitName
                this.dataForm.status = data.groupDesc.status
                this.dataForm.backDesc = data.groupDesc.backDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tgroup/groupdesc/${!this.dataForm.descId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'descId': this.dataForm.descId || undefined,
                'userId': this.dataForm.userId,
                'userName': this.dataForm.userName,
                'submoney': this.dataForm.submoney,
                'joinTime': this.dataForm.joinTime,
                'phone': this.dataForm.phone,
                'groupId': this.dataForm.groupId,
                'unitId': this.dataForm.unitId,
                'unitName': this.dataForm.unitName,
                'status': this.dataForm.status,
                'backDesc': this.dataForm.backDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
